package com.rollCall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rollCall.entity.CheckInRecord;
import com.rollCall.entity.Class;
import com.rollCall.entity.No;
import com.rollCall.entity.Student;
import com.rollCall.entity.vo.ClazzStudentVo;
import com.rollCall.entity.vo.StudentVo;
import com.rollCall.mapper.CheckInRecordMapper;
import com.rollCall.mapper.ClassMapper;
import com.rollCall.mapper.NoMapper;
import com.rollCall.mapper.StudentMapper;
import com.rollCall.service.StudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.rollCall.common.constant.MessageConstant.DEFAULT_PASSWORD;

/**
 *  
 * @date 2025/2/5
 */
@Service
@RequiredArgsConstructor
public class StudentServiceImpl implements StudentService {
	private final StudentMapper studentMapper;
	private final ClassMapper classMapper;
	private final NoMapper noMapper;
	private final CheckInRecordMapper checkInRecordMapper;

	@Override
	public StudentVo getStudentById(Long id) {
		Student student = studentMapper.selectById(id);
		StudentVo studentVo = new StudentVo();
		BeanUtil.copyProperties(student, studentVo);
		studentVo.setClassName(classMapper.selectById(student.getClassId()).getName());
		return studentVo;
	}

	@Override
	public List<Student> getAllStudents(Long classId) {
		QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("id", "uid", "name");
		if (classId != null) {
			queryWrapper.eq("class_id", classId);
		}
		return studentMapper.selectList(queryWrapper);
	}

	@Override
	public void addStudent(ClazzStudentVo clazzStudentVo) {
		Long classId = clazzStudentVo.getId();
		clazzStudentVo.getStudentVoList().forEach(
				classStudent -> {
					Student student = new Student();
					if (classStudent.getUid() == null) {
						throw new RuntimeException("学生学号不能为空");
					}
					if (studentMapper.selectCount(new QueryWrapper<Student>().eq("uid", classStudent.getUid())) != 0) {
						throw new RuntimeException("学号重复");
					}
					student.setUid(classStudent.getUid());
					student.setClassId(classId);
					student.setName(classStudent.getName());
					student.setPassword(DEFAULT_PASSWORD);
					studentMapper.insert(student);
				}
		);

	}

	@Override
	public void deleteStudent(List<Long> ids) {
		ids.forEach(id -> {
			Integer studentId = checkInRecordMapper.selectCount(new QueryWrapper<CheckInRecord>().eq("student_id", id));
			if (studentId != 0) {
				throw new RuntimeException("存在签到记录，无法删除");
			}
		});
		studentMapper.deleteBatchIds(ids);
	}

	@Override
	public List<Student> findByClassId(int classId) {
		QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
		if (classId != 0) {
			queryWrapper.eq("class_id", classId);
		}
		return studentMapper.selectList(queryWrapper);
	}

	@Override
	public Long getNo(Long classId) {
		List<Student> studentList = studentMapper.selectList(new QueryWrapper<Student>().eq("class_id", classId));
		Long noId = RandomUtil.randomLong(10000000, 99999999);
		studentList.forEach(student -> {
			No no = new No();
			no.setNo(noId);
			no.setClassId(classId);
			no.setStudentId(student.getId());
			no.setName(student.getName());
			noMapper.insert(no);
		});
		return noId;
	}
}
